﻿using Client.Model;
using Client.Service;
using Client.View;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Client.ViewModel
{
    public class MenuItemViewModel : BindableBase
    {
        /// <summary>
        /// 主菜单
        /// </summary>
        private MainMenu menu;

        public MainMenu Menu
        {
            get { return menu; }
            set
            {
                menu = value;
                RaisePropertyChanged("Menu");
            }
        }



        /// <summary>
        /// 是否显示扩展列表（有子列表的时候展示）
        /// </summary>
        private Visibility expanderVisibility = Visibility.Collapsed;

        public Visibility ExpanderVisibility
        {
            get { return expanderVisibility; }
            set
            {
                expanderVisibility = value;
                RaisePropertyChanged("ExpanderVisibility");
            }
        }

        /// <summary>
        /// 是否显示主菜单名称，不显示扩展列表（没有子菜单）
        /// </summary>
        private Visibility mainMenuVisibility = Visibility.Visible;

        public Visibility MainMenuVisibility
        {
            get { return mainMenuVisibility; }
            set
            {
                mainMenuVisibility = value;
                RaisePropertyChanged("MainMenuVisibility");
            }
        }

        
        /// <summary>
        /// 选中的子菜单
        /// </summary>
        private SubMenu selectedSubMenuItem;

        public SubMenu SelectedSubMenuItem
        {
            get { return selectedSubMenuItem; }
            set
            {
                selectedSubMenuItem = value;
                RaisePropertyChanged("SelectedSubMenuItem");
            }
        }



        public DelegateCommand SelectSubMenuCommand { get; private set; }
        public DelegateCommand ClickMainMenuCommand { get; private set; }


        public MainWindowViewModel MainDataContext { get; set; }

        public MainMenuItem MainItem { get; set; }

        public MenuItemViewModel(MainMenu menu, MainWindowViewModel mainWindowDataContext, MainMenuItem mainMenuItem)
        {
            MainItem = mainMenuItem;
            MainDataContext = mainWindowDataContext;
            Menu = menu;
            if (menu.SubMenus != null && menu.SubMenus.Count > 0)
            {
                ExpanderVisibility = Visibility.Visible;
                MainMenuVisibility = Visibility.Collapsed;
            } else
            {
                ExpanderVisibility = Visibility.Collapsed;
                MainMenuVisibility = Visibility.Visible;
            }
            InitCommands();
        }

        private void InitCommands()
        {
            SelectSubMenuCommand = new DelegateCommand(SelectSubMenuEvent);
            ClickMainMenuCommand = new DelegateCommand(ClickMainMenuEvent);
        }

        /// <summary>
        /// 选择子菜单事件
        /// </summary>
        private void SelectSubMenuEvent()
        {
            if (selectedSubMenuItem == null) return;
            UserControl screen = ScreenService.Instance().GetScreenPage(selectedSubMenuItem.Screen);
            if (screen != null)
            {
                MainDataContext.MainPage = screen;
            }
            MainItem.ResetListSelected();
        }


        private void ClickMainMenuEvent()
        {
            UserControl screen = ScreenService.Instance().GetScreenPage(Menu.Screen);
            if (screen != null)
            {
                MainDataContext.MainPage = screen;
            }
        }



    }
}
